from django.shortcuts import render
from django.http import HttpResponse
import json
from django.views.decorators.csrf import csrf_exempt
from django.forms import inlineformset_factory
from findlove.forms import CandidatePhotoForm, CandidateForm
from findlove.models import Candidate, CandidatePhoto
from utils.upload_to_cloud import upload_to_qiniu
import os

# Create your views here.
# 加个装饰器绕过csrf验证

current_dir = os.path.dirname(os.path.dirname(__file__))


@csrf_exempt
def candidate_view(request):

    if request.method == 'POST':

        candidate_form = CandidateForm(request.POST, request.FILES)
        if candidate_form.is_valid():

            new_candidate = candidate_form.save(commit=False)
            new_candidate.save()

            # 获取一下刚刚保存的这个candidate id，后面保存对应的photo需要这个id对应的candidate实体
            new_candidate_id = new_candidate.id
            new_candidate = Candidate.objects.get(id=new_candidate_id)
            # 刚刚保存一下是为了把图片先上传到本地，然后获取本地地址，再上传到七牛云
            uploader_campus_card_local_file = current_dir + new_candidate.uploader_campus_card.url
            # 想办法获取到form上传过来的image，在传给七牛云并获得外链存入数据库中
            if upload_to_qiniu(uploader_campus_card_local_file):
                uploader_campus_card_url = upload_to_qiniu(uploader_campus_card_local_file)
            else:
                return HttpResponse('upload picture error.')
            # 得到了七牛云的链接我们就存入数据库
            new_candidate.uploader_campus_card_url = uploader_campus_card_url
            new_candidate.save()

            candidate_photo_files = request.FILES.getlist('candidate_photo')

            # 终于解决了附表上传保存的问题，这里candidate的photo保存在一个附表中，也就是说一个candidate可以有
            # 多张candidate photo记录与之对应
            # 接下来我把它上传到七牛云上返回一个链接看看
            for i, f in enumerate(candidate_photo_files):
                candidate_photo_form = CandidatePhotoForm()
                new_candidate_photo = candidate_photo_form.save(commit=False)
                new_candidate_photo.candidate = new_candidate
                new_candidate_photo.candidate_photo = f
                new_candidate_photo.save()

                new_candidate_photo_id = new_candidate_photo.id
                new_candidate_photo = CandidatePhoto.objects.get(id=new_candidate_photo_id)
                candidate_photo_local_file = current_dir + new_candidate_photo.candidate_photo.url
                if upload_to_qiniu(candidate_photo_local_file):
                    candidate_photo_url = upload_to_qiniu(candidate_photo_local_file)
                else:
                    return HttpResponse('upload picture error.')
                new_candidate_photo.candidate_photo_url = candidate_photo_url
                new_candidate_photo.save()

            response = {
                'status': 'success',
                'message': 'fuck damn it done!'
            }

            return HttpResponse(json.dumps(response))

        return HttpResponse('form is not valid.')
    else:
        return HttpResponse('only support post method!!!')

